package org.jsbd.boss.action;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.jsbd.boss.domian.ChannelProvinceInfo;
import org.jsbd.boss.domian.ProvinceInfo;
import org.jsbd.boss.domian.UmConfigInfo;
import org.jsbd.boss.domian.UmConfigProvince;
import org.jsbd.boss.service.IApkLoginTypeService;
import org.jsbd.boss.service.IApkProvinceService;
import org.jsbd.boss.service.IApkSimulatorService;
import org.jsbd.boss.service.IApkTaskQueryService;
import org.jsbd.boss.service.IIpService;
import org.jsbd.boss.service.IUmConfigInfoService;
import org.jsbd.boss.service.IUmConfigProvinceService;
import org.jsbd.boss.annotation.SystemControllerLog;
import org.jsbd.boss.common.DataGridModel;
import org.jsbd.boss.common.ResponseMapUtils;
import org.jsbd.boss.common.ResponseUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.common.utils.StringUtils;

@Controller
@RequestMapping("/umConfigInfo")
public class UmConfigInfoController {

	public static final Logger logger = LoggerFactory.getLogger(UmConfigInfoController.class);

	@Autowired
	private IUmConfigInfoService umConfigInfoService;

	@Autowired
	private IUmConfigProvinceService umConfigProvinceService;

	@Resource
	private IIpService ipService;

	@RequestMapping(value = "list", method = RequestMethod.GET)
	public String list(Model model) {
		return "umConfigInfo/list";
	}

	@RequestMapping(value = "save", method = RequestMethod.POST)
	@ResponseBody
	public Object save(UmConfigInfo umConfigInfo) {
		if (StringUtils.isBlank(umConfigInfo.getChannel())) {
			return ResponseMapUtils.responseFail("渠道不能为空");
		}
		umConfigInfoService.save(umConfigInfo);
		return ResponseMapUtils.responseSuccess();
	}

	@RequestMapping(value = "update", method = RequestMethod.POST)
	@ResponseBody
	public Object update(UmConfigInfo umConfigInfo) {
		if (umConfigInfo.getId() == null) {
			return ResponseMapUtils.responseFail("参数缺失，请确认参数再提交");
		}
		UmConfigInfo obj = umConfigInfoService.getById(umConfigInfo.getId());
		obj.setAppInfoId(umConfigInfo.getAppInfoId());
		obj.setAppId(umConfigInfo.getAppId());
		obj.setChannel(umConfigInfo.getChannel());
		obj.setBrushCount(umConfigInfo.getBrushCount());
		obj.setOpenStatus(umConfigInfo.getOpenStatus());
		obj.setOnlineTime(umConfigInfo.getOnlineTime());
		obj.setNextDayRetention(umConfigInfo.getNextDayRetention());
		obj.setOpenProvince(umConfigInfo.getOpenProvince());
		obj.setRetentOpenStatus(umConfigInfo.getRetentOpenStatus());
		umConfigInfoService.update(obj);
		return ResponseMapUtils.responseSuccess();
	}

	@RequestMapping(value = "getListPage")
	@ResponseBody
	@SystemControllerLog(description = "获取友盟配置信息")
	public Map<String, Object> getListPage(DataGridModel page, UmConfigInfo umConfigInfo) throws Exception {
		return umConfigInfoService.getListPage(page, umConfigInfo);
	}

	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public void delete(Integer[] ids, HttpServletResponse response) {
		try {
			List<Integer> list = Arrays.asList(ids);
			umConfigInfoService.delete(list);
			ResponseUtils.responseSuccess(response);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			ResponseUtils.responseFailure(response);
		}
	}

	@RequestMapping(value = "{configInfoId}/provinceList")
	public String provinceList(@PathVariable String configInfoId, Model model) {
		List<ProvinceInfo> list = ipService.getProvinceInfoList();
		List<UmConfigProvince> selectedList = umConfigProvinceService.getByConfigInfoId(Integer.valueOf(configInfoId));

		Set<Integer> provinceSet = new HashSet<>();
		for (UmConfigProvince umConfigProvince : selectedList) {
			provinceSet.add(umConfigProvince.getProvinceId());
		}

		if (list != null && list.size() > 0) {
			Iterator<ProvinceInfo> itetator = list.iterator();
			while (itetator.hasNext()) {
				ProvinceInfo province = itetator.next();
				Integer provinceId = province.getId();
				if (provinceId.intValue() == -1) {
					itetator.remove();
				} else {
					if (provinceSet.contains(provinceId)) {
						province.setChecked(true);
					} else {
						province.setChecked(false);
					}
				}
			}
		}
		model.addAttribute("provinceList", list);
		return "umConfigInfo/province";
	}

	@RequestMapping(value = "saveProvince", method = RequestMethod.POST)
	@SystemControllerLog(description = "省份保存")
	public void saveProvince(String configInfoId, String openProvince, HttpServletResponse response) {
		try {
			umConfigProvinceService.updateProvinceByConfigInfoId(configInfoId, openProvince);
			ResponseUtils.responseSuccess(response);
		} catch (Exception e) {
			ResponseUtils.responseFailure(response);
		}
	}
}